package ${config.packageName}${module}<#noparse>.</#noparse>${config.serviceImpl.packageName};

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import ${config.packageName}.core.utils.BeanUtils;

import ${config.packageName}${module}<#noparse>.</#noparse>${config.po.packageName}<#noparse>.</#noparse>${config.po.namePrefix!''}${table.className}${config.po.nameSuffix!''};
import ${config.packageName}${module}<#noparse>.</#noparse>${config.dto.packageName}<#noparse>.</#noparse>${config.dto.namePrefix!''}${table.className}${config.dto.nameSuffix!''};
import ${config.packageName}${module}<#noparse>.</#noparse>${config.query.packageName}<#noparse>.</#noparse>${config.query.namePrefix!''}${table.className}${config.query.nameSuffix!''};
import ${config.packageName}${module}<#noparse>.</#noparse>${config.request.packageName}<#noparse>.</#noparse>${config.request.namePrefix!''}${table.className}${config.request.nameSuffix!''};

import ${config.packageName}${module}<#noparse>.</#noparse>${config.mapper.packageName}<#noparse>.</#noparse>${config.mapper.namePrefix!''}${table.className}${config.mapper.nameSuffix!''};
import ${config.packageName}${module}<#noparse>.</#noparse>${config.serviceApi.packageName}<#noparse>.</#noparse>${config.serviceApi.namePrefix!''}${table.className}${config.serviceApi.nameSuffix!''};


@Slf4j
@Service
public class ${config.serviceImpl.namePrefix!''}${table.className}${config.serviceImpl.nameSuffix!''} implements ${config.serviceApi.namePrefix!''}${table.className}${config.serviceApi.nameSuffix!''} {

    @Autowired
    private ${config.mapper.namePrefix!''}${table.className}${config.mapper.nameSuffix!''} ${table.className?uncap_first}${config.mapper.nameSuffix!''};

    @Override
    public Long save(${config.request.namePrefix!''}${table.className}${config.request.nameSuffix!''} request){
        ${table.className}PO ${table.className?uncap_first}PO = BeanUtil.copyProperties(request, ${table.className}PO.class);

        this.${table.className?uncap_first}Mapper.insertNotNull(${table.className?uncap_first}PO);

        return ${table.className?uncap_first}PO.getId();
    }

    @Override
    public Boolean update(Long id, ${config.request.namePrefix!''}${table.className}${config.request.nameSuffix!''} request){

        ${table.className}PO ${table.className?uncap_first}PO = BeanUtil.copyProperties(request, ${table.className}PO.class);
        ${table.className?uncap_first}PO.setId(id);

        return this.${table.className?uncap_first}Mapper.updateNotNull(${table.className?uncap_first}PO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.${table.className?uncap_first}Mapper.deleteById(id);
    }

    @Override
    public ${config.dto.namePrefix!''}${table.className}${config.dto.nameSuffix!''} queryById(Long id){

        ${table.className}PO ${table.className?uncap_first}PO = this.${table.className?uncap_first}Mapper.selectById(id);

        ${table.className}DO ${table.className?uncap_first}DO = BeanUtil.copyProperties(${table.className?uncap_first}PO, ${table.className}DO.class);

        return ${table.className?uncap_first}DO;
    }


    @Override
    public List<${config.dto.namePrefix!''}${table.className}${config.dto.nameSuffix!''}> queryList(${config.query.namePrefix!''}${table.className}${config.query.nameSuffix!''} request){

        Condition condition = request.toCondition();

        List<${table.className}PO> poList = this.${table.className?uncap_first}Mapper.selectList(condition);

        List<${table.className}DO> doList = BeanUtil.copyToList(poList, ${table.className}DO.class);

        return doList;
    }

    @Override
    public BasePager<${config.dto.namePrefix!''}${table.className}${config.dto.nameSuffix!''}> queryPager(${config.query.namePrefix!''}${table.className}${config.query.nameSuffix!''} request){


        PageInfo<${table.className}PO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.${table.className?uncap_first}Mapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, ${table.className}DO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.${table.className?uncap_first}Mapper.deleteByIds(ids);
    }


}
